<template>
  <el-form label-width="100px" label-position="left">
    <el-form-item label="图层名称" prop="layerName">
      <el-input
        v-model.trim="formData.layerName"
        size="default"
        style="width: 100%"
        @change="changed($event, 'layerName')"
      />
    </el-form-item>
    <el-form-item label="背景颜色" prop="backgroundColor">
      <ColorPicker
        v-model.trim="formData.backgroundColor"
        @change="(val) => changed(val, 'backgroundColor')"
      />
    </el-form-item>
  </el-form>
</template>
<script lang="ts" setup>
import { reactive, watch } from 'vue'
import ColorPicker from '../../components/colorPicker.vue'

// 接收父组件传入的值
const props = defineProps<{
  modelValue: any
}>()

const emit = defineEmits(['update:modelValue', 'onChanged'])

const formData = reactive<any>({})

watch(
  () => props.modelValue,
  (newVal) => {
    Object.assign(formData, newVal)
  },
  { immediate: true, deep: true }
)

const changed = (val: any, key: string) => {
  emit('update:modelValue', { ...formData })
  emit('onChanged', { key, val })
}
</script>
<style lang="scss" scoped></style>
